package com.mzy.linear_struct.面试150题;

import javax.lang.model.element.VariableElement;

/**
 * @Author: mizy3
 * @Date: 2024/08/16/下午2:24
 * @Description:
 */
public class Pro14最长公共前缀 {

    public String longestCommonPrefix(String[] strs) {
        if(strs.length <= 1) return strs[0];
        String str = strs[0];
        String ans = str;
        for (int i = 1; i < strs.length; i++) {
            int j = 0;
            for (; j < strs[i].length() && j < str.length(); j++) {
                if (strs[i].charAt(j) != str.charAt(j)) {
                    break;
                }
            }
            String s = str.substring(0, j);
            ans = ans.length() > s.length() ? s : ans;
        }
        return ans;
    }


    public static void main(String[] args) {
        new Pro14最长公共前缀().longestCommonPrefix(new String[]{"aaa", "aa", "aaa"});
    }
}
